'''
Created on Oct 9, 2011

@author: solagna
'''
import pickle
import bdiiList
import WebDownloader.webPage
import json
from time import sleep

class avCompare(object):
    '''
    classdocs
    '''
    __month__ = 0
    __days__ = [999,31,28,31,30,31,30,31,31,30,31,30,31]
    __siteROCav__ = {} # {"site":{"day_of_the_week":"1.00"},{...}}
    __siteROCre__ = {}
    __siteWLCGav__ = {}
    __siteWLCGre__ = {}
    __timestamps__ = set()
    __sites__ = []
    __sites_list_URL__ = "http://grid-monitoring.cern.ch/myegi/sam-pi/site_names?infrast_name=EGI&certified=y&output=json"
    __samURL_WLCG__    = "http://grid-monitoring.cern.ch/myegi/sam-pi/group_availability_in_profile?vo_name=ops&profile_name=WLCG_CREAM_LCGCE_CRITICAL&start_time=2011-11-14T00:00:00Z&end_time=2011-12-04T23:59:59Z&type=WEEKLY&group_type=Site&group_name=%s&output=JSON"
    __samURL_ROC__     = "http://grid-monitoring.cern.ch/myegi/sam-pi/group_availability_in_profile?vo_name=ops&profile_name=ROC_CRITICAL&start_time=2011-11-14T00:00:00Z&end_time=2011-12-04T23:59:59Z&type=WEEKLY&group_type=Site&group_name=%s&output=JSON"
    def __init__(self):
        '''
        Constructor
        '''
        #Populate the sites.
        wPage = WebDownloader.webPage.webPage(self.__sites_list_URL__)
        sites_data = json.loads(wPage.getBody())
        for one_site in sites_data:
            self.__sites__.append(one_site['name'])                
        #Populate A/R
        #check Dump fies
        dump_wlcg_name = "aval_roc.pickle"
        dump_wlcgr_name = "aval_wlcg.pickle"
        dump_roc_name = "rel_roc.pickle"
        dump_rocr_name = "rel_wlcg.pickle"
        
        try:
            #Try dump files
            roc_file = open(dump_roc_name, 'r')
            roc_filer= open(dump_rocr_name, 'r')
            wlcg_file=open(dump_wlcg_name,'r')
            wlcg_filer=open(dump_wlcgr_name,'r')
            
            self.__siteROCav__ = pickle.load(roc_file)
            self.__siteROCre__ = pickle.load(roc_filer)
            self.__siteWLCGav__ = pickle.load(wlcg_file)
            self.__siteWLCGre__ = pickle.load(wlcg_filer)

            print "Warning sites availabilities/reliabilities not fresh!"
            roc_file.close()
            roc_filer.close()
            wlcg_file.close()
            wlcg_filer.close()
        except:
            print "Getting fresh data!"
            print "URL saved in urls.list"
            urls_list = open("urls.list","w")
            prob_log = open("compare-problems.log","w")
            num_sites = len(self.__sites__)
            num_site = 0
            for single_site in self.__sites__:
                num_site += 1
                if (num_site % 100==0):
                    print "So far: %d in ROC and %d in WLCGWait for 1min..."%(len(self.__siteROCav__),len(self.__siteWLCGav__))
                    sleep(300)
                print "Site %d of %d"%(num_site,num_sites)
                warn_string = "Populate site %s ..."%single_site
                print warn_string,
                try:
                    (url1,url2,) = self.populateSiteAval(single_site)
                    urls_list.write(url1+"  ****  "+url2+"\n")
                except:
                    #Save urls anyway.
                    urls_list.write("Problem: "+self.buildWLCGurl(single_site)+"  ****  "+self.buildROCurl(single_site))
                    print "warning no data ",
                    prob_log.write(single_site+"did not inserted\n")
                print "...done"
            urls_list.close()
            prob_log.close()
            #serialize objects
            roc_file = open(dump_roc_name, 'w')
            roc_filer= open(dump_rocr_name, 'w')
            wlcg_file= open(dump_wlcg_name,'w')
            wlcg_filer= open(dump_wlcgr_name,'w')
            pickle.dump(self.__siteROCav__,roc_file)
            pickle.dump(self.__siteROCre__,roc_filer)
            pickle.dump(self.__siteWLCGav__,wlcg_file)
            pickle.dump(self.__siteWLCGre__,wlcg_filer)
            roc_file.close() 
            roc_filer.close()
            wlcg_file.close()
            wlcg_filer.close()
            
            
            
    def buildWLCGurl(self,site):
        return self.__samURL_WLCG__%site
    def buildROCurl(self,site):
        return self.__samURL_ROC__%site
        
    def populateSiteAval(self,site):
        wlcg_url = self.buildWLCGurl(site)
        roc_url  = self.buildROCurl(site)
        #WLCG/ROC data
        (siteROCav, siteROCre) = self.calculateGenericAv(site,roc_url)
        (siteWLCGav,siteWLCGre) = self.calculateGenericAv(site,wlcg_url)
        self.__siteROCav__[site]=siteROCav 
        self.__siteROCre__[site]=siteROCre
        self.__siteWLCGav__[site]=siteWLCGav
        self.__siteWLCGre__[site]=siteWLCGre
        
        return (wlcg_url,roc_url)
        
    def calculateCompTableAvg (self):
        print "Site,WLCG av, WLCG rel, ROC av, ROC rel"
        for site in self.__sites__:
            if site in self.__siteROCav__:
                print site+",",
                print "%f,"%self.calculateAvg(self.__siteWLCGav__[site]),
                print "%f,"%self.calculateAvg(self.__siteWLCGre__[site]),
                print "%f,"%self.calculateAvg(self.__siteROCav__[site]),
                print "%f,"%self.calculateAvg(self.__siteROCre__[site])




    def calculateGenericAv (self,site,URL):
        wPage = WebDownloader.webPage.webPage(URL)
        sites_data = json.loads(wPage.getBody())  
        av_list = {}
        rel_list= {}  
        for week_av in sites_data[0]['groups'][0]['availabilities']:
            timestamp = week_av['timestamp']
            av = week_av['availability']
            rel = week_av['reliability']
            av_list[timestamp]=av
            rel_list[timestamp]=rel
            self.__timestamps__.add(timestamp) #Create set of timestamps, for debug purposes.
        return (av_list,rel_list)  
            

    def calculateAvg (self,data):
        total = 0
        weeks = 0
        for timestamp in data.keys():
            total += float(data[timestamp])
            weeks += 1
        return total/weeks
    
if __name__ == "__main__":
    #Download all.
    #x.printPage()
    av = avCompare()

    #print "###"
    #print av.calculateAvg(av.__siteROCav__['INFN-ROMA2'])
    av.calculateCompTableAvg()
    print av.__siteROCav__['INFN-ROMA2'].keys()
    
    #print av.buildHourURL(9, "bdii-fzk.gridka.de")
    #(bdii,aval) = av.searchHourlyBDII('bdii-fzk.gridka.de')
    #print bdii
    #print aval
    #for a in aval.keys():
    #   print "%s %s"%(a,aval[a])  
